#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
from PIL import Image, ImageEnhance
import numpy as np

from skimage import io
from skimage import transform
from skimage.color import rgb2gray, gray2rgb

import numpy as np

import matplotlib.pyplot as plt


def blackwhite(name):
    original = io.imread(name)

    grayscale = rgb2gray(original)

    ts = 97 / 255
    mask = grayscale > ts
    grayscale[mask] = 1.0
    mask = grayscale <= ts
    grayscale[mask] = 0.0
    grayscale = 1.0 - grayscale
    return grayscale


def margin(im, top=0, bottom=30):
    h = im.shape[0] + top + bottom
    r = np.zeros((h, im.shape[1], im.shape[2]))
    r[top : im.shape[0] + top, :, :] = im[:, :, :]
    return r


def main(input_path):
    # Get file name without extension
    # base_name = os.path.splitext(os.path.basename(input_path))[0]

    copied = blackwhite(input_path)
    ch, cw = copied.shape
    cw = int(1500.0 / ch * cw + 0.5)
    ch = 1500
    copied = transform.resize(copied, (ch, cw))
    copied = gray2rgb(copied) * 255
    copied = margin(copied, 20, 20)
    ch = copied.shape[0]
    cw = copied.shape[1]
    plt.figure()
    plt.imshow(copied)
    plt.show()


if __name__ == "__main__":
    filename = "/Users/zoushengyuan/prog/py/duobaota/001.jpg"
    main(filename)
